package app.biz.config;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private TypeResolver resolver;

    @Value("${swagger.enabled}")
    String swaggerEnabled;

    @Bean
    public Docket api() {
        //未启用swagger
        if (!Boolean.valueOf(swaggerEnabled)) {
            // 不生成api
            return  new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("nothing")).build();
        }

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
//                .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("app.biz.controller"))
//                .paths(PathSelectors.any())
                .paths(or(regex("/api/.*")))//过滤的接口
                .build()
                /** 配合ApiImplicitParams注解可以用于显示map类型的json对象
                 * 但仍无法显示example数据
                 * //    @ApiImplicitParams({
                 * //            @ApiImplicitParam(
                 * //                    name = "ecgObject",
                 * //                    type = "body",
                 * //                    dataType = "EcgObject"
                 * //            )
                 * //    })
                 */
                // 对Map(Json)类型提交显示参数相关的Example内容
//                .additionalModels(resolver.resolve(EcgObject.class))
                ;
    }
}